package Puzzle;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class Puzzle {

	private String nom;
	private BufferedImage image;
	private ArrayList<Piece> listePieces;

	public Puzzle(String nom, File nomImage) throws IOException {
		this.nom = nom;
		this.image = ImageIO.read(nomImage);
		this.listePieces = new ArrayList<Piece>();
	}

	public int recupererPiece() {
		int numeroPiece =1;
		int[][] res = new int[this.image.getWidth()][this.image.getHeight()];
		int i = 0;
		int j=0;
		for (i = 0; i < this.image.getWidth(); i++) {
			for (j = 0; j < this.image.getHeight(); j++) {
				Color c = new Color(this.image.getRGB(i, j));
				if (c.getBlue() == 255 && c.getRed() == 255
						& c.getGreen() == 255) {
					res[i][j] = 1;
					this.image.setRGB(i,j,0);
				} else {
					res[i][j] = 0;
				}

			}
		}
		for (i = 1; i < this.image.getWidth(); i++) {
			for (j = 1; j < this.image.getHeight(); j++) {
				int haut = 1, bas=1, droite=1, gauche=1;
				boolean fini = false;
				int[][] res2 = null;
				if(res[i][j]==0){
					while( fini == false){
						int w = i - haut;
						int x = j - gauche;
						res2= new int[gauche+droite+1][haut+bas+1];

						for(int k = 0; k < droite+gauche+1; k++){
							for(int l = 0; l < haut+bas+1; l++){
								res2[k][l]=res[w][x];
								w++;
							}
							x++;
							w = i - haut;
						}
						fini = true;
						if(!verifierHaut(res2)){
							haut++;
							fini = false;
						}
						if(!verifierbas(res2)){
							bas++;
							fini = false;
						}
						if(!verifierGauche(res2)){
							gauche++;
							fini = false;
						}
						if(!verifierDroite(res2)){
							droite++;
							fini = false;
						}
					}
					int largeur = j+droite - (j-gauche);
					int hauteur = i+bas - (i-haut);
					BufferedImage test = image.getSubimage(i-haut, j-gauche, hauteur, largeur);
					Piece p = new Piece(test, numeroPiece);
					listePieces.add(p);
					numeroPiece ++;
					for(int k = i-haut; k < i+bas; k++){
						for(int l = j - gauche ; l < j+droite; l++){
							res[k][l]=1;
						}
					}
				}
				
			}
		}
		
		return i;
		
	}

	
	private boolean verifierHaut(int mat[][]){
		boolean res = true;
		int i=0;
		while(i<mat.length && res){
			if(mat[i][0] == 0){
				res = false;
			}
			i++;
		}
		return res;
	}
	
	private boolean verifierbas(int mat[][]){
		boolean res = true;
		int i=0;
		while(i<mat.length && res){
			if(mat[i][mat[0].length-1] == 0){
				res = false;
			}
			i++;
		}
		return res;
	}
	
	private boolean verifierGauche(int mat[][]){
		boolean res = true;
		int i=0;
		while(i<mat[0].length && res){
			if(mat[0][i] == 0){
				res = false;
			}
			i++;
		}
		return res;
	}
	
	private boolean verifierDroite(int mat[][]){
		boolean res = true;
		int i=0;
		while(i<mat[0].length && res){
			if(mat[mat.length-1][i] == 0){
				res = false;
			}
			i++;
		}
		return res;
	}
	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public BufferedImage getImage() {
		return image;
	}

	public void setImage(BufferedImage image) {
		this.image = image;
	}

	public ArrayList<Piece> getListePieces() {
		return listePieces;
	}

	public void setListePieces(ArrayList<Piece> listePieces) {
		this.listePieces = listePieces;
	}

}
